/* 4 buyers */

foreach buyer in "Georgia" "Caltrans" "Malta" "Mongolia" { 
	di "*************** `buyer' *****************"
			if "`buyer'" == "Georgia"{
			use ../data/georgia/sample_georgia26may22.dta, clear /* NAT==1only */
			qui gen delta_low1 = delta_first
			qui gen delta_low2 = delta23_first
			qui gen delta_win1 = deltaw_first
			qui gen delta_win2 = deltaw23_first
			
			cap drop pct_bid 
			g pct_bid = pct_lowestbid
			g bid_rank = bid_rank_first 
			
		}
		else if "`buyer'" == "Caltrans"{
			use ../data/caltrans/sample_caltrans29Dec24.dta, clear
			rename delta delta_low1
			rename delta23 delta_low2

		}
		else if "`buyer'" == "Malta"{
			local TABLE_combined = ""
			use ../data/Malta/malta.dta, clear
			keep if Evaluation_Method == "Price/Cost Effectiveness" & eAuctions=="No"
		    rename deltap delta_win1
			rename delta2p delta_win2
		    rename delta delta_low1
			rename delta2 delta_low2
			
			tab Procurement_Type, gen(temp)
			replace winner = 0 if delta_low1 !=. & winner ==.

		}
		else if "`buyer'" == "Mongolia"{				
			use ../data/mongolia/sample_mongolia_30jun22.dta, clear
			rename delta1w delta_win1
			rename delta2w delta_win2
			rename delta1 delta_low1
			rename delta2 delta_low2
	
		}
		
		cap drop flag_tmp
			gen flag_tmp = 0
			sum delta_low1 if delta_low1 < 0, d	
			replace flag_tmp = 1 if delta_low1 < `r(p10)'
			sum delta_low1 if delta_low1 > 0, d
			replace flag_tmp = 1 if delta_low1 > `r(p90)'

		/*Fixed Effects Estimates for Log Future Award Amount*/ 
		/*full sample*/  
		reghdfe lnflog30 winner if flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model1_`buyer'
		reghdfe lnflog60 winner if flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model2_`buyer'
		reghdfe lnflog90 winner if flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model3_`buyer'
		reghdfe lnflog120 winner if flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model4_`buyer' 

		if ("`buyer'" == "Georgia" | "`buyer'" == "Caltrans") {
		
		qui reghdfe lnflog30 winner if flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		g sample = e(sample)
		
		sum pct_bid if bid_rank == 1 & sample == 1, d 
		g median = r(p50)
		g temp = (pct_bid < median) if bid_rank == 1 & sample == 1
		tab temp 
		bys contract_id: egen sample_comp = max(temp) if sample == 1
		bys contract_id: g N_participants = _N 
		bys temp: sum N_participants
		
		/*Competitive auctions sample*/
		reghdfe lnflog30 winner if sample_comp == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model5_`buyer'
		reghdfe lnflog60 winner if sample_comp == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model6_`buyer'
		reghdfe lnflog90 winner if sample_comp == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model7_`buyer'
		reghdfe lnflog120 winner if sample_comp == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model8_`buyer'	
		}

		if ("`buyer'" == "Malta") {
			
		/*Public Auctions sample in Malta*/  	
		reghdfe lnflog30 winner if temp3 == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model5_`buyer'
		reghdfe lnflog60 winner if temp3 == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model6_`buyer'
		reghdfe lnflog90 winner if temp3 == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model7_`buyer'
		reghdfe lnflog120 winner if temp3 == 1 & flag_tmp == 0 & delta_low1 != 0 , absorb(firm_id) cluster(contract_id)
		est sto model8_`buyer'
		
	}
} 

*Table OC.2: Fixed Effects Estimates for Log Future Award Amount: Georgia, Mongolia, Malta and California
local full_results1 "model1_Georgia model2_Georgia model3_Georgia model4_Georgia model1_Caltrans model2_Caltrans model3_Caltrans model4_Caltrans"		
local full_results2 "model1_Malta model2_Malta model3_Malta model4_Malta model1_Mongolia model2_Mongolia model3_Mongolia model4_Mongolia"
		
			esttab `full_results1' using ../Output/TableOC2.tex, ///
				f replace cells(b(fmt(3) star) se(fmt(3) par)) nonumbers /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) ///
				collabels(none) ///
				keep(winner) order(winner) varlabels(winner "\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}") ///
				mtitles("30 Days" "60 Days" "90 Days" "120 Days" "30 Days" "60 Days" "90 Days" "120 Days") stats(N, fmt(0) labels("Obs.")) ///
				posthead(\\[-2mm] &\multicolumn{4}{c}{Georgia} & \multicolumn{4}{c}{Caltrans} \\ ///
				 \cmidrule(l){2-5}   \cmidrule(l){6-9})

			esttab `full_results2' using ../Output/TableOC2.tex, ///
				f append cells(b(fmt(3) star) se(fmt(3) par)) nonumbers /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) ///
				collabels(none) ///
				keep(winner) order(winner) varlabels(winner  "\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}") ///
				mtitles("30 Days" "60 Days" "90 Days" "120 Days" "30 Days" "60 Days" "90 Days" "120 Days") stats(N, fmt(0) labels("Obs.")) ///
				posthead(\\[-2mm] &\multicolumn{4}{c}{Malta} & \multicolumn{4}{c}{Mongolia} \\ ///
							 \cmidrule(l){2-5}   \cmidrule(l){6-9})

*Table OC.3: Fixed Effects Estimates for Log Future Award Amount: Georgia and Caltrans
local comp_results1 "model5_Georgia model6_Georgia model7_Georgia model8_Georgia"		
local comp_results2 "model5_Caltrans model6_Caltrans model7_Caltrans model8_Caltrans"
								 
			esttab `comp_results1' using ../Output/TableOC3.tex,  ///
				f replace cells(b(fmt(3) star) se(fmt(3) par)) nonumbers /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) /// 
				collabels(none) ///
				keep(winner) order(winner) varlabels(winner "\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}") ///
				prehead(&\multicolumn{4}{c}{Georgia - Competitive Auctions}) ///
				mtitles("30 Days" "60 Days" "90 Days" "120 Days") ///
				stats(N, fmt(0) labels("Obs."))

			esttab `comp_results2' using ../Output/TableOC3.tex,  ///
				f append cells(b(fmt(3) star) se(fmt(3) par)) nonumbers /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) /// 
				collabels(none) ///
				keep(winner) order(winner) varlabels(winner "\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}") ///
				prehead(&\multicolumn{4}{c}{Caltrans - Competitive Auctions}) ///
				mtitles("30 Days" "60 Days" "90 Days" "120 Days")  /// 
				stats(N, fmt(0) labels("Obs."))
				
*Table OC.4: Fixed Effects Estimates for Log Future Award Amount: Public Works Auctions from Malta.
local works_results "model5_Malta model6_Malta model7_Malta model8_Malta"

			esttab `works_results' using ../Output/TableOC4.tex,  ///
				f replace cells(b(fmt(3) star) se(fmt(3) par)) nonumbers /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) /// 
				collabels(none) ///
				keep(winner) order(winner) varlabels(winner "\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}") ///
				prehead(&\multicolumn{4}{c}{Malta - Works}) ///
				mtitles("30 Days" "60 Days" "90 Days" "120 Days") stats(N, fmt(0) labels("Obs."))
